from flask_mqtt import Mqtt
from config import MQTT_CONFIG

def setup_mqtt(app):
    app.config.update(MQTT_CONFIG)
    mqtt = Mqtt(app)
    # 订阅 fulai 主题
    mqtt.subscribe(MQTT_CONFIG['MQTT_TOPIC'])
    data_list = []  # 用于存储接收到的数据
    # 处理接收到的 MQTT 消息
    @mqtt.on_message()
    def handle_mqtt_message(client, userdata, message):
        data = message.payload
        process_data(data)
    def process_data(data):
        chunk_size = 3000  # 每个分片的大小
        while len(data) > 0:
            chunk = data[:chunk_size]
            data_list.append(chunk)
            data = data[chunk_size:]
        print(f"接收到的数据段数量: {len(data_list)}") 
    return data_list